package android.medicalcloud.com.yiapp.yilian.ui.ac;

import android.graphics.Color;
import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.activity.BaseActivity;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.medicalcloud.com.yiapp.yilian.configer.enums.HttpConfig;
import android.medicalcloud.com.yiapp.yilian.logic.UserController;
import android.medicalcloud.com.yiapp.yilian.model.UserModel;
import android.medicalcloud.com.yiapp.yilian.ui.view.CommonTitleView;
import android.medicalcloud.com.yiapp.yilian.utils.android.GsonTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.ImageLoader;
import android.medicalcloud.com.yiapp.yilian.utils.android.IntentTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.JsonTool;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;

/**
 * Created by wanggenlin on 17-5-19.
 */
public class PersonDetialActivity extends BaseActivity implements ViewNetCallBack{
    CommonTitleView titleView;
    ImageView avatarIv;
    TextView  codeTv,nameTv,sexTv,hospitalTv,departmentTv,technologyTv,learnTv,titleRightTv;
    EditText expertTv,summaryTv;
    @Override
    protected void initView() {
        titleView =(CommonTitleView) findViewById(R.id.titleView);
        titleView.setTitleText("个人详情");
        avatarIv = (ImageView)findViewById(R.id.avatarIv);
        codeTv = (TextView)findViewById(R.id.codeTv);
        nameTv = (TextView)findViewById(R.id.nameTv);
        sexTv = (TextView)findViewById(R.id.sexTv);
        hospitalTv = (TextView)findViewById(R.id.hospitalTv);
        departmentTv = (TextView)findViewById(R.id.departmentTv);
        technologyTv = (TextView)findViewById(R.id.technologyTv);
        learnTv = (TextView)findViewById(R.id.learnTv);
        expertTv = (EditText)findViewById(R.id.expertTv);
        summaryTv = (EditText)findViewById(R.id.summaryTv);
        titleRightTv = (TextView)findViewById(R.id.titleRightTv);
        findViewById(R.id.avatarLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (model==null){
                    return;
                }
                IntentTool.startAvatar(PersonDetialActivity.this,model.getImg());

            }
        });
        titleRightTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (titleRightTv.getText().toString().contains("编")){
                    expertTv.setEnabled(true);
                    summaryTv.setEnabled(true);
                    titleRightTv.setText("保   存");
                    expertTv.setBackgroundColor(Color.parseColor("#ffffff"));
                    summaryTv.setBackgroundColor(Color.parseColor("#ffffff"));

                }else{
                    if (model==null){
                        return;
                    }
                    if (StringTools.isNullOrEmpty(expertTv.getText().toString())){

                        Toast.makeText(PersonDetialActivity.this,"个人擅长不能为空",Toast.LENGTH_LONG).show();
                        return;
                    }
                    if (expertTv.getText().length()>=300){
                        Toast.makeText(PersonDetialActivity.this,"个人擅长不能超过300字符",Toast.LENGTH_LONG).show();
                        return;
                    }

                    if (StringTools.isNullOrEmpty(summaryTv.getText().toString())){
                        Toast.makeText(PersonDetialActivity.this,"个人简介不能为空",Toast.LENGTH_LONG).show();
                        return;
                    }
                    if (summaryTv.getText().length()>=1000){
                        Toast.makeText(PersonDetialActivity.this,"个人简介不能超过1000字符",Toast.LENGTH_LONG).show();
                        return;
                    }

                    UserController.getInstance().updateInfor(PersonDetialActivity.this,expertTv.getText().toString(),summaryTv.getText().toString());
                }

            }
        });
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        UserController.getInstance().detailInfor(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.ac_person;
    }


    @Override
    public void onConnectStart(Object o) {

    }

    @Override
    public void onConnectEnd() {

    }

    @Override
    public void onFail(Exception e, Object o, String reslut) {

    }

    @Override
    public void onData(Serializable result, int flag, boolean fromNet, Object o) {
        Logger.e(o.toString());
        if (flag== HttpConfig.userDetailInfor.getType()){
            try {
                model = GsonTool.jsonToEntity(JsonTool.getString(o.toString(),"user"), UserModel.class);

                // ImageView avatarIv;
                //TextView nameTv,hospitalTv,incomeTv;
                if (!StringTools.isNullOrEmpty(model.getImg())){
                    ImageLoader.getInstance(this).displayCileImage(model.getImg(),avatarIv);
                }
                nameTv.setText(model.getName());
                hospitalTv.setText(model.getHospital());
                nameTv.setText(model.getName());
                sexTv.setText(model.getSex());
                departmentTv.setText(model.getDepartment());
                technologyTv.setText(model.getTechnology());
                learnTv.setText(model.getLearning());
                expertTv.setText(model.getExpert());
                summaryTv.setText(model.getSummy());
            }catch (Exception E){

            }
        }

        if (flag==HttpConfig.userUpdate.getType()){
            if (JsonTool.getString(o.toString(),"status").equals("1")){
                return;
            }

            expertTv.setEnabled(false);
            summaryTv.setEnabled(false);
            titleRightTv.setText("编  辑");
            expertTv.setBackgroundColor(Color.parseColor("#00000000"));
            summaryTv.setBackgroundColor(Color.parseColor("#00000000"));

        }
    }
    UserModel model ;
}
